home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CGAMEJOY.ARJ / TESTMID.C < prev    next >
C/C++ Source or Header  |  1992-01-08  |  10KB  |  269 lines

  1. /* testmid.c
  2.                 Test for middle level joystick procedures in joystkmd.c.
  3.                 Quits if no joystick is detected at port 201h.
  4.  
  5.                 written for Borland C++ 2.0
  6.  
  7.                 to complile :
  8.                             bcc testmid.c joystkmd.c joystklo.c
  9.                         or
  10.                             bcc -ms testmid.c joystk_s.lib
  11.  
  12.                 by Ed Torgerson : CIS 70313,456,  GEnie E.TORGERSON1.
  13.                 any constructive correspondence is welcome.
  14.  
  15. -----------------------------------------------------------------------------*/
  16.  
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <dos.h>
  20. #include "joystick.h"
  21.  
  22. /*-- Global variables -----*/
  23.  
  24. void NotifyUser ( int msg ) ;
  25.  
  26. void NotifyUser (int msg)
  27. {               
  28.     switch (msg)
  29.     {
  30.         case  -2:
  31.             printf ( "warning: joystick installation incomplete!!!!!\n\n" ) ;
  32.             break ;
  33.         case  -1:
  34.             printf ( "joystick installation terminated by user!!!!\n\n" ) ;
  35.             break ;
  36.         case   0:
  37.             printf ( "no joystick detected\n\n" ) ;
  38.             break ;
  39.         case   1:
  40.             printf ( "press escape to quit joystick initialization.\n\n" ) ;
  41.             break ;
  42.         case   2:
  43.             printf ( "center joystick and press button\n\n" ) ;
  44.             break ;
  45.         case   3:
  46.             printf ( "move joystick to top-left and press button 1\n\n" ) ;
  47.             break ;
  48.         case   4:
  49.             printf ( "move joystick to lower-right and press button 2\n\n" ) ;
  50.             break ;
  51.         case  -3:
  52.         default :
  53.             break ;
  54.     }
  55.     return ;
  56. }
  57.  
  58. void  main(void)
  59. {
  60.     struct JoystickPosition jpos ;
  61.     int     i, tolerence ;
  62.  
  63.     i = 0 ;
  64.  
  65.     clrscr () ;
  66. /*------------------ Notice -----------------------------*/
  67.  
  68.     printf ("\tDemonstration program for the joystick support functions\n") ;
  69.     printf ("\tin joystklo.c, joystkmd.c, and joystick.h.  This program \n") ;
  70.     printf ("\twill quit if no joystick is detected.  The functions in \n") ;
  71.     printf ("\tjoystklo.c provide mixed language support at the port level.\n") ;
  72.     printf ("\tThe functions in joystkmd.c provide C language middle level\n") ;
  73.     printf ("\ttranslation of the low level output.\n\n" ) ;
  74.     printf ("\tThe features of this library include screening for changed\n");
  75.     printf ("\tstate events only, position/resistance scaling for non-linear\n");
  76.     printf ("\tjoystick output, rough translation from the top-left origin\n" );
  77.     printf ("\tjoystick coordinate system to a fairly balanced 0-centered\n");
  78.     printf ("\tcartesian system, and valid axis detection.\n\n" ) ;
  79.     printf ("\tThis library is organized in an Object-Ready fashion.Inter-\n");
  80.     printf ("\tmodule data accesses are hidden by class-style support\n" ) ;
  81.     printf ("\tfunctions.  C++ mavens should have little difficulty in\n" ) ;
  82.     printf ("\ttranslating this library to classes.\n\n" ) ;
  83.     printf ("\t\tEd Torgerson  CIS 70313,456,  GEnie E.TORGERSON1\n\n" ) ;
  84.     printf ("\tFor a look at pure low level C Functions, see the file in\n" ) ;
  85.     printf ("\tBorland's C forum on which my low level functions are based:\n");
  86.     printf ("\t\tJOY.ARC written by Gary Blaine  72707,1736\n" ) ;
  87.     printf ("\t\t\tany key to continue...") ;
  88.     getch() ;
  89.     clrscr () ;
  90.  
  91. /*------------------ Initialization test -----------------------------*/
  92.     if ( JstInitialize ( NotifyUser ) == 0 )
  93.     {
  94.         printf( "Initialize failed or was terminated!!!\n\n" ) ;
  95.         return ;
  96.     }          
  97.     printf( "Initialize succeeded!\n\n" ) ;
  98.     i = JstGetActiveAxes() ;
  99.     if ( (i & JAX_AXIS) != 0 )
  100.             printf("\tJoystick A x-axis is valid.\n" );
  101.     if ( (i & JAY_AXIS) != 0 )
  102.             printf("\tJoystick A y-axis is valid.\n" );
  103.     if ( (i & JBX_AXIS) != 0 )
  104.             printf("\tJoystick B x-axis is valid.\n" );
  105.     if ( (i & JBY_AXIS) != 0 )
  106.             printf("\tJoystick B y-axis is valid.\n\n" );
  107.  
  108.     printf ( "\nG_Stick_Centers - " ) ;
  109.     JstGetCenter( JOYSTICK_A, &jpos) ;
  110.     printf ("jax: %04d,  jay: %04d,  ", jpos.jx, jpos.jy);
  111.     JstGetCenter( JOYSTICK_B, &jpos) ;
  112.     printf ("jbx: %04d,  jby: %04d\n", jpos.jx, jpos.jy);
  113.  
  114.     printf ( "\nG_Stick_Minimums - " ) ;
  115.     JstGetMin( JOYSTICK_A, &jpos) ;
  116.     printf ("jax: %04d,  jay: %04d,  ", jpos.jx, jpos.jy);
  117.     JstGetMin( JOYSTICK_B, &jpos) ;
  118.     printf ("jbx: %04d,  jby: %04d\n", jpos.jx, jpos.jy);
  119.  
  120.     printf ( "\nG_Stick_Maximums - " ) ;
  121.     JstGetMax( JOYSTICK_A, &jpos) ;
  122.     printf ("jax: %04d,  jay: %04d,  ", jpos.jx, jpos.jy);
  123.     JstGetMax( JOYSTICK_B, &jpos) ;
  124.     printf ("jbx: %04d,  jby: %04d\n", jpos.jx, jpos.jy);
  125.  
  126.     tolerence = JstGetTolerance() ;
  127.     printf ( "\nG_Stick_Tolerence - %04d\n", tolerence ) ;
  128.  
  129.     printf ("\nany key to continue...\n") ;
  130.     getch() ;
  131. /*------------------ Middle Level tests -----------------------------*/
  132.  
  133.  
  134.     clrscr() ;
  135.     printf("JstGetChangedButton() returns button values") ;
  136.     printf(" only if they've changed.\n") ;
  137.     printf("JstGetChangedPosition() returns the current position") ;
  138.     printf(" only if it has changed.\n") ;
  139.     printf("JstXlatToCart() translates the current position") ;
  140.     printf(" to cartesian coordinates.\n") ;
  141.     printf("J or j sets the center to the current location. JstSetCenter()\n") ;
  142.     printf("M or m sets the minimum to the current location. JstSetMin()\n") ;
  143.     printf("N or n sets the maximum to the current location. JstSetMax()\n") ;
  144.     printf("+ or - increments or decrements the tolerence. JstSetTolerance()") ;
  145.     printf("\n\n\t\tmove joystick/press buttons.\n") ;
  146.     printf("\n\t\t\t\t\tpresss ESCAPE to quit...\n") ;
  147.  
  148.     delay (100) ;
  149.  
  150.     tolerence = tolerence >> 1 ;        /* set tolerence by half steps */
  151.  
  152. do_test_loop:
  153.  
  154.     while( !kbhit() )
  155.     {
  156.     /*----- Button Changed Status Test --------------------------------------*/
  157.         i = JstGetChangedButton () ;
  158.  
  159.         if ( (i & JA1_BUTTON_DOWN) != 0 )
  160.             printf("Joystick A Button 1 down\n" );
  161.         if ( (i & JA1_BUTTON_UP  ) != 0 )
  162.             printf("Joystick A Button 1     up\n" );
  163.         if ( (i & JA2_BUTTON_DOWN) != 0 )
  164.             printf("Joystick A Button 2 down\n" );
  165.         if ( (i & JA2_BUTTON_UP  ) != 0 )
  166.             printf("Joystick A Button 2     up\n" );
  167.         if ( (i & JB1_BUTTON_DOWN) != 0 )
  168.             printf("Joystick B Button 1 down\n" );
  169.         if ( (i & JB1_BUTTON_UP  ) != 0 )
  170.             printf("Joystick B Button 1     up\n" );
  171.         if ( (i & JB2_BUTTON_DOWN) != 0 )
  172.             printf("Joystick B Button 2 down\n" );
  173.         if ( (i & JB2_BUTTON_UP  ) != 0 )
  174.             printf("Joystick B Button 2     up\n" );
  175.         delay ( 6 ) ;
  176.  
  177.     /*----- A Position Test -----------------------------------------------*/
  178.  
  179.         if ( JstGetChangedPosition(JOYSTICK_A, &jpos) )
  180.         {
  181.             printf("Joystick A      \t   %3dx    %3dy real", jpos.jx, jpos.jy) ;
  182.             JstXlatToCart (JOYSTICK_A, &jpos) ;
  183.             printf("\t   %4dx    %4dy cartesian\n", jpos.jx, jpos.jy) ;
  184.         }
  185.         delay ( 6 ) ;
  186.  
  187.     /*----- B Position Test -----------------------------------------------*/
  188.         if ( JstGetChangedPosition(JOYSTICK_B, &jpos) )
  189.         {
  190.             printf("Joystick B      \t   %3dx    %3dy real", jpos.jx, jpos.jy) ;
  191.             JstXlatToCart (JOYSTICK_B, &jpos) ;
  192.             printf("\t   %4dx    %4dy cartesian\n", jpos.jx, jpos.jy) ;
  193.         }
  194.  
  195.         delay ( 6 ) ;
  196.     }
  197.     i = getch () ;
  198.     if ( (i == 'j') || (i == 'J') )
  199.     {
  200.         printf ("\n\n\t\tResetting center position of both joysticks.\n") ;
  201.         JstSetCenter( JOYSTICK_A ) ;
  202.         JstSetCenter( JOYSTICK_B ) ;
  203.         JstGetCenter( JOYSTICK_A, &jpos ) ;
  204.         printf ( "New Centers at - " ) ;
  205.         printf ("jx: %04d,  jy: %04d,  ", jpos.jx, jpos.jy);
  206.         JstGetCenter( JOYSTICK_B, &jpos) ;
  207.         printf ("jx: %04d,  jy: %04d\n\n", jpos.jx, jpos.jy);
  208.         goto do_test_loop ;
  209.     }
  210.     if ( (i == 'n') || (i == 'N') )
  211.     {
  212.         printf ("\n\n\t\tResetting minimum position of both joysticks.\n") ;
  213.         JstSetMin( JOYSTICK_A ) ;
  214.         JstSetMin( JOYSTICK_B ) ;
  215.         JstGetMin( JOYSTICK_A, &jpos) ;
  216.         printf ( "New Minimums at - " ) ;
  217.         printf ("jx: %04d,  jy: %04d,  ", jpos.jx, jpos.jy);
  218.         JstGetMin( JOYSTICK_B, &jpos) ;
  219.         printf ("jx: %04d,  jy: %04d\n\n", jpos.jx, jpos.jy);
  220.         goto do_test_loop ;
  221.     }
  222.     if ( (i == 'm') || (i == 'M') )
  223.     {
  224.         printf ("\n\n\t\tResetting maximum position of both joysticks.\n") ;
  225.         JstSetMax( JOYSTICK_A ) ;
  226.         JstSetMax( JOYSTICK_B ) ;
  227.         JstGetMax( JOYSTICK_A, &jpos) ;
  228.         printf ( "New Maximums at - " ) ;
  229.         printf ("jx: %04d,  jy: %04d,  ", jpos.jx, jpos.jy);
  230.         JstGetMax( JOYSTICK_B, &jpos) ;
  231.         printf ("jx: %04d,  jy: %04d\n\n", jpos.jx, jpos.jy);
  232.         goto do_test_loop ;
  233.     }
  234.     else if ( i == '+' )
  235.     {
  236.         i = JstGetTolerance() ;
  237.         if ( i < tolerence * 20 )
  238.         {
  239.             i += tolerence ;
  240.             JstSetTolerance( i ) ;
  241.             printf("\n\tJoystick tolerence now set to %d\n\n", i ) ;
  242.         }
  243.         goto do_test_loop ;
  244.     }
  245.     else if ( i == '-' )
  246.     {
  247.         i = JstGetTolerance() ;
  248.         if ( i > tolerence )
  249.         {
  250.             i -= tolerence ;
  251.             JstSetTolerance( i ) ;
  252.             printf("\n\tJoystick tolerence now set to %d\n\n", i ) ;
  253.         }
  254.         goto do_test_loop ;
  255.     }
  256.     if ( (i == 59) )
  257.     {
  258.         clrscr () ;
  259.         printf("\n\n\tj:center,\tm:minimum,\tn:maximum,\t+/-:tolerance\n") ;
  260.         printf("\n\t\t\tPress escape to quit.\n\n") ;
  261.         goto do_test_loop ;
  262.     }
  263.     if ( (i != 27) )
  264.         goto do_test_loop ;
  265.     return ;
  266.  
  267. }
  268. /*-- end testmid.c */
  269.